package com.ls.kdlss.ui.home;

import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gm.lib.utils.GMPopup;
import com.ls.kdlss.R;
import com.wx.android.common.util.ActivityUtil;


import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AnswerActivity extends AppCompatActivity {


    @Bind(R.id.rl_view)
    RelativeLayout rlView;
    @Bind(R.id.ib_back)
    ImageButton ibBack;

    public static void launch(Context context) {

        ActivityUtil.startActivity(context, AnswerActivity.class);
    }

    @Bind(R.id.vp_content)
    ViewPager vpContent;
    @Bind(R.id.activity_answer)
    LinearLayout activityAnswer;
    @Bind(R.id.tabl_view)
    TabLayout tablView;
    @Bind(R.id.ll_view)
    LinearLayout llView;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.iv_status)
    ImageView ivStatus;
    @Bind(R.id.ll_title)
    LinearLayout llTitle;


    private List<Fragment> data;
    private static List<String> data2;

    static {
        data2 = new ArrayList<>();
        data2.add("最新");
        data2.add("最热");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_answer);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {


        llTitle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                ivStatus.setBackgroundColor(0xfff);
                MyFilter popupWindow = new MyFilter(AnswerActivity.this);
                popupWindow.showPopupWindow(rlView);
               /* GMPopup.setBackgroundAlpha(AnswerActivity.this, 1f);*/
                popupWindow.setOnChoose(new MyFilter.OnChoose() {
                    @Override
                    public void Selected(String grade, String subjecte) {
                        tvTitle.setText(grade + subjecte);
                    }
                });

                popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
                    @Override
                    public void onDismiss() {
                        GMPopup.setBackgroundAlpha(AnswerActivity.this, 1f);
                       /* ivStatus.setImageResource(R.mipmap.triangle);*/
                    }
                });

            }
        });


        data = new ArrayList<>();
        AnswerFragment fragment1 = AnswerFragment.getInstance("1");
        AnswerFragment fragment2 = AnswerFragment.getInstance("2");
        data.add(fragment1);
        data.add(fragment2);

        tablView.setTabMode(TabLayout.MODE_FIXED);
        tablView.addTab(tablView.newTab().setText(data2.get(0)));
        tablView.addTab(tablView.newTab().setText(data2.get(1)));
        vpContent.setAdapter(new PagerAdapter(getSupportFragmentManager()));
        tablView.setupWithViewPager(vpContent);

    }

    @OnClick(R.id.ib_back)
    public void onClick() {
        finish();
    }

    class PagerAdapter extends FragmentPagerAdapter {

        public PagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return data.get(position);
        }

        @Override
        public int getCount() {
            return 2;
        }

        //此方法用来显示tab上的名字
        @Override
        public CharSequence getPageTitle(int position) {

            return data2.get(position % data2.size());
        }
    }
}
